//
//  GeRenJiShiViewController.m
//  EJL
//
//  Created by adc on 15/11/8.
//  Copyright © 2015年 江苏一键联信息科技有限公司. All rights reserved.
//

#import "GeRenJiShiViewController.h"
#import "GRJSTableViewCell.h"
//#import "HttpManage.h"
//#import "GRJS.h"

//#import "towGRJS.h"
//#import "thire.h"
#import "gerenjsTableViewCell.h"
#import "WoDeWeiDianViewController.h"
#import "HttpsManager.h"
#import "TwoGeRenJiShi.h"
#import "thireGeRenJiShi.h"
#import "MyShopVC.h"
@interface GeRenJiShiViewController ()

@end

@implementation GeRenJiShiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"我的微店"]];
//    UIView*view=[[UIView alloc]initWithFrame:CGRectMake(0, 20, self.view.frame.origin.x, 44)];
//    [self.view addSubview:view];
//    UIImageView*bJ=[[UIImageView alloc]initWithFrame:CGRectMake(0, 20, self.view.frame.origin.x, 44)];
//    bJ.image=[UIImage imageNamed:@""]
    [HttpsManager requestToGetGRJSCompletion:^(GeRenJiShi *gr)
    {
        if (gr!=nil)
        {
            _leftArray=gr.leftAry;
            _rightArray=gr.rightAry;
            _otherAry=gr.otherAry;
            _gR=gr;
            __weak GeRenJiShiViewController *weakSelf = self;
            [_tableView addPullToRefreshWithActionHandler:^{
                NSLog(@"当进行下拉刷新执行该方法");
                [weakSelf performSelector:@selector(requestEnd) withObject:nil afterDelay:1.0f];
            }];
            
            [_tableView addInfiniteScrollingWithActionHandler:^{
                [weakSelf performSelector:@selector(requestEnd) withObject:nil afterDelay:1.0f];
            }];

            [_tableView reloadData];
        }
        else
        {
            LoginVC*vc=[[LoginVC alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
        
    }];
    UIView*view=[[UIView alloc]initWithFrame:CGRectMake(0, 20,WIDE , 44)];
    view.backgroundColor=RGB_COLOR(0x00c49f);
    [self.view addSubview:view];
    UILabel*label=[[UILabel alloc]initWithFrame:CGRectMake(WIDE/2-35, 10, 70, 24)];
    label.text=@"个人集市";
    label.textColor=[UIColor whiteColor];
    [view addSubview:label];
    UIButton*but1=[[UIButton alloc]initWithFrame:CGRectMake(10,7,30,30)];
    [but1 setImage:[UIImage imageNamed:@"返回箭头"] forState:UIControlStateNormal];
    [but1 addTarget:self action:@selector(backButClick) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:but1];
    _tableView.tableFooterView=[[UIView alloc]init];
    [_tableView registerNib:[UINib nibWithNibName:@"GRJSTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell1"];
    [_tableView registerNib:[UINib nibWithNibName:@"gerenjsTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell2"];
   //进入我的微店按钮
}
- (void)requestEnd{
    [_tableView.pullToRefreshView stopAnimating];
    
    [_tableView.infiniteScrollingView stopAnimating];
    
}
-(void)backButClick
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)butClick
{
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    WoDeWeiDianViewController*vc=[[WoDeWeiDianViewController alloc]init];
//    [self.navigationController pushViewController:vc animated:YES];
    
    
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _leftArray.count+1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0)
    {
        return 70;
    }
    if (indexPath.row==0)
    {
        return 70;
    }
    return 200;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section==0)
    {
        return 1;
    }
    else
    {
         return 2;
    }
   
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0)
    {
        
        gerenjsTableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"cell2" forIndexPath:indexPath];
        [cell.tuPian sd_setImageWithURL:[NSURL URLWithString:_gR.avatar] placeholderImage:[UIImage imageNamed:@"头像"]];
        cell.nameLabel.text=_gR.shopName;
        
       [cell.jinRu addTarget:self action:@selector(jinRu) forControlEvents:UIControlEventTouchUpInside];
        return cell;
    }
    
    if (indexPath.row==0)
    {
        thireGeRenJiShi*thire=_otherAry[indexPath.section-1];
        
        gerenjsTableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"cell2" forIndexPath:indexPath];
        cell.nameLabel.text=thire.shopName;
        
        [cell.jinRu setTitle:@"进入店铺" forState:UIControlStateNormal];
        //cell.imageView.image=[UIImage imageNamed:@"我的头像2.jpg"];
       [ cell.tuPian sd_setImageWithURL:[NSURL URLWithString:thire.shopAvatar] placeholderImage:[UIImage imageNamed:@"头像"]];
        
        return cell;
    }
    TwoGeRenJiShi*twoo=_leftArray[indexPath.section-1];
    
    GRJSTableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"cell1" forIndexPath:indexPath];
   cell.name1.text=twoo.name;
    if (twoo.discount)
    {
       // NSString*str=[NSString stringWithFormat:@"%d",]
        if ([twoo.discount  isKindOfClass:[NSNull class]])
        {
          cell.jiaGe.text=[NSString stringWithFormat:@"%.2f",twoo.price*0.01];
       }
        else
        {
//            NSString*disStr=[NSString stringWithFormat:@"%d",twoo.discountPrice];
//            if ([disStr  isKindOfClass:[NSNull class]])
//            {
//                cell.jiaGe.text=@"";
//            }
//            else
//            {
               // NSString*str=[NSString stringWithFormat:@"%d",twoo.discountPrice];
                cell.jiaGe.text=[NSString stringWithFormat:@"%.2f",twoo.discountPrice*0.01];
           // }
            
        }
        
    }
    //if (![twoo.picture  isKindOfClass:[NSNull class]])
    //{
         [cell.tuPian1 sd_setImageWithURL:[NSURL URLWithString:twoo.picture] placeholderImage:[UIImage imageNamed:@"头像"]];
    //}
   
   // NSString*yj=
    cell.yongJin.text=[NSString stringWithFormat:@"%d",twoo.commision];
    TwoGeRenJiShi*twooo=_rightArray[indexPath.section-1];
    cell.label2.text=twooo.name;
    if (twooo.discount)
    {
        

                cell.jiaGe.text= [NSString stringWithFormat:@"%d",twooo.discountPrice];
       
        
    }
    
    [cell.tuPian2 sd_setImageWithURL:[NSURL URLWithString:twooo.picture] placeholderImage:[UIImage  imageNamed:@"头像"]];
  
    cell.yongJin2.text=[NSString stringWithFormat:@"%d",twooo.commision];

    
    return cell;
    }
    
//    towGRJS*tow=_quArray[indexPath.section];
//    _cellArray=tow.array;
//    thire*th=_cellArray[indexPath.section];
//    //GRJS*gr=_shopListArray[indexPath.section];
//    [cell.imageView1 setImageWithURL:[NSURL URLWithString:th.imgUrl]];
//    [cell.imageView2 setImageWithURL:[NSURL URLWithString:th.imgUrl]];
//    cell.name1.text=th.title;
//    cell.label2.text=th.title;
//    cell.jiaGe.text=th.Price;
//    cell.jiaGe2.text=th.Price;
//    cell.yongJin.text=th.brokerage;
//    cell.yongJin2.text=th.brokerage;
    //return cell;

//-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//{
//    UITableViewHeaderFooterView*headView=[tableView dequeueReusableHeaderFooterViewWithIdentifier:@"section"];
//    if (!headView)
//    {
//        headView=[[UITableViewHeaderFooterView alloc]initWithReuseIdentifier:@"section"];
//        UIImageView*image=[[UIImageView alloc]initWithFrame:CGRectMake(10, 5,40, 40)];
//        image.clipsToBounds=YES;
//        image.layer.cornerRadius=20;
//        image.tag=1;
//        [headView addSubview:image];
//        UILabel*label=[[UILabel alloc]initWithFrame:CGRectMake(60, 10, 60, 20)];
//        label.tag=2;
//        [headView addSubview:label];
//         UIButton*but=[[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width-70, 10, 60, 20)];
//        but.tag=3;
//        
//        [headView addSubview:but];
//        
//        
//    }
//    towGRJS*tow=_quArray[section];
//    
//    UIImageView*image=(UIImageView*)[headView viewWithTag:1];
//    [image setImageWithURL:[NSURL URLWithString:tow.avatar]];
//    
//    UILabel*label=(UILabel*)[headView viewWithTag:2];
//    label.text=tow.name;
    
//    UIButton*but=(UIButton*)[headView viewWithTag:3];
//    [but setBackgroundImage:[UIImage imageNamed:@"进入店铺"] forState:UIControlStateNormal];
//    [but addTarget:self action:@selector(but) forControlEvents:UIControlEventTouchUpInside];
//    return headView;
//}
-(void)jinRu
{
    
    MyShopVC *myShop = [[MyShopVC alloc] init];
    
    myShop.shopId=[NSString stringWithFormat:@"%d",_gR.shopId];
    [self.navigationController pushViewController:myShop animated:YES];
}
//进入店铺按钮
-(void)but
{
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
